﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting.Channels;
using TekaMUD.Engine.Blocks;

namespace TekaMUD.Engine.IO
{
	/// <summary>
	/// Handles worldwide communications.
	/// </summary>
	public class Messenger
	{
		/// <summary>
		/// Sends a message to a particular agent.
		/// </summary>
		public static void SendAgent(Player agent, string message)
		{
			agent.Channel.WriteString(message);
		}

		/// <summary>
		/// Sends a message to all the players in a particular room.
		/// </summary>
		public static void SendRoom(Room room, string message)
		{
			Player[] list = World.Current.GetPlayersByRoom(room);

			foreach (Player p in list)
				if(p.Status == PlayerStatus.Active && p.Channel.Status == ChannelStatusEnum.Opened)
					p.Channel.WriteString(message);
		}

		/// <summary>
		/// Sends a message to all the players in a particular room, except a particular player.
		/// </summary>
		public static void SendRoom(Room room, string message, Player exemptPlayer)
		{
			Player[] list = World.Current.GetPlayersByRoom(room);

			foreach (Player p in list)
				if(p != exemptPlayer)
					p.Channel.WriteString(message);
		}

		/// <summary>
		/// Sends a message to all the human players in the world.
		/// </summary>
		/// <param name="message"></param>
		public static void Broadcast(string message)
		{
			Player[] list = EntityManager.GetAllPlayers();

			foreach (Player p in list)
				if(p.Status == PlayerStatus.Active)
					p.Channel.WriteString(message);
		}

		/// <summary>
		/// Sends a message to a particular room and adjacent rooms, up to a specified depth or distance.
		/// </summary>
		public static void SendRegion(Room room, uint depth, string message)
		{
			throw new NotImplementedException();
		}
	}
}
